
package fluebot;

import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.PircBot;

/**
 *
 * @author Aksel Kvitberg
 */
public class Fluebot extends PircBot{

	HashMap<String, ChannelHandler> channels = new HashMap<String, ChannelHandler>();
	String channelRegex = "[#&\\+][\\w0-9\\{\\}\\-]+";

	public Fluebot(String server){
		// Connect to the given server. User controll instance for each server.
		this.setName("Fluebot");
		this.setLogin("fluebot");
		this.setVersion("0.1");
		try {
			this.connect(server);
		} catch (IOException ex) {
			Logger.getLogger(Fluebot.class.getName()).log(Level.SEVERE, null, ex);
		} catch (IrcException ex) {
			Logger.getLogger(Fluebot.class.getName()).log(Level.SEVERE, null, ex);
		}
		//Bot should be connected to server now.
		// Connect to a initial set of channels
		connectToChannels();
		//Bot should now be in the initial set of channels
	}

	Pattern channelPattern = Pattern.compile(channelRegex);
	private void connectToChannels(){
		//TODO: Decide which channels to initially connect to. Probably through a file, no?
		String text = IO.readAllText("channels.txt");
		Matcher m = channelPattern.matcher(text);
		while (m.find()){
			//Found a valid channel. Make a handler for that channel
			connectToChannel(m.group());
		}
	}

	public void onMessage(String channel, String sender, String login, String hostname, String message) {
		ChannelHandler handler = channels.get(channel);
//		System.out.println(handler);
		handler.onMessage(sender, login, hostname, message);
	}

	public void onKick(String channel, String kickerNick, String kickerLogin, String kickerHostname, String recipientNick, String reason){
		if (recipientNick.equalsIgnoreCase(this.getNick())){
			ChannelHandler handler = channels.get(channel);
			handler.rejoin();
			handler.onMessage(kickerNick, kickerLogin, kickerHostname, "!attack me");
		}
	}

	public void onAction(String sender, String login, String hostname, String target, String action) {
		if (this.getName().equalsIgnoreCase(target)){
			//PM'ed at us
		} else {
			ChannelHandler handler = channels.get(target);
			handler.onAction(sender, login, hostname, target, action);
		}
	}

	public static void main(String args[]){
		new Fluebot("irc.lessthan3.net");
	}

	public void joinNewChannel(String channel) {
		if(channel.matches(channelRegex)){
			connectToChannel(channel);
		}
	}

	public void addNewChannel(String channel){
		if(channel.matches(channelRegex)){
			//Write to channelfile
			connectToChannel(channel);
		}
	}

	public void leaveChannel(String channel){
		if(channel.matches(channelRegex)){
			ChannelHandler h = channels.get(channel);
			h.dispose();
			h = null;
			this.partChannel(channel);
		}
	}

	private void connectToChannel(String channel) {
			System.out.println(channel);
			ChannelHandler bot = new ChannelHandler(channel, this);
			channels.put(channel, bot);
	}
}
